using Protocol;
using Protocol.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class AccountHandler : HandlerBase
{
    public override void OnReceive(int subCode, object value)
    {
        switch (subCode)
        {
            case AccountCode.LOGIN:
                loginResponse((int)value);
                break;
            case AccountCode.REGIST_SRES:
                registResponse((int)value);
                break;

            default:
                break;
        }
    }

    PromptMsg PromptMsg = new PromptMsg();
    /// <summary>
    /// 登录响应
    /// </summary>
    private void loginResponse(int result)
    {
        switch (result)
        {
            case 0:
                // 跳转场景
                LoadSceneMsg msg = new LoadSceneMsg(1, delegate ()
                {
                    //向服务器获取自身信息
                    SocketMsg socketMsg = new SocketMsg(OpCode.USER, UserCode.GET_INFO_CREQ, null);
                    Dispatch(AreaCode.NET, 0, socketMsg);
                    //Debug.Log("加载完成!");
                });
                
                Dispatch(AreaCode.Scene, SceneEvent.LOAD_SCENE, msg);
                break;
            case -1:
                PromptMsg.Change("账号不存在", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
                break;
            case -2:
                PromptMsg.Change("账号在线", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
                break;
            case -3:
                PromptMsg.Change("账号密码不匹配", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
                break;
            default:
                break;
        }

        //if(result == "登陆成功")
        //{
        //    PromptMsg.Change(result.ToString(), Color.green);
        //    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
        //    // 跳转场景
        //    // TODO

        //    return;
        //}

        //PromptMsg.Change(result.ToString(), Color.red);
        //Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
    }

    /// <summary>
    /// 注册响应
    /// </summary>
    private void registResponse(int result)
    {
        switch (result)
        {
            case 0:
                PromptMsg.Change("注册成功", Color.green);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
                break;
            case -1:
                PromptMsg.Change("账号已经存在", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
                break;
            case -2:
                PromptMsg.Change("账号输入不合法", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
                break;
            case -3:
                PromptMsg.Change("密码不合法", Color.red);
                Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
                break;
            default:
                break;
        }
        //if (result == "注册成功")
        //{
        //    PromptMsg.Change(result.ToString(), Color.green);
        //    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
        //    // 跳转场景
        //    // TODO

        //    return;
        //}

        //PromptMsg.Change(result.ToString(), Color.red);
        //Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, PromptMsg);
    }
}
